package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.controller.admin.SetmealController;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //分页查询
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<Setmeal> page= setmealMapper.pageQuery(setmealPageQueryDTO);
        PageResult pageResult=new PageResult();
        List<Setmeal> result = page.getResult();
        List<SetmealVO> collect = result.stream().map(setmeal -> {
            SetmealVO setmealVO = new SetmealVO();
            BeanUtils.copyProperties(setmeal, setmealVO);
            setmealVO.setCategoryName(setmealMapper.getById(setmeal.getId()).getName());
            return setmealVO;
        }).collect(Collectors.toList());
        pageResult.setTotal(page.getTotal());
        pageResult.setRecords(collect);
        return pageResult;
    }

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Transactional
    public void insert(SetmealDTO setmealDTO) {

        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        List<SetmealDish> setmealDishes=setmealDTO.getSetmealDishes();
        setmealMapper.insert(setmeal);
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmeal.getId());
            setmealMapper.insertSetmealDish(setmealDish);
        }
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    public SetmealVO getById(Long id) {
        Setmeal setmeal=setmealMapper.getById(id);
        if(setmeal!=null){
            SetmealVO setmealVO=new SetmealVO();
            BeanUtils.copyProperties(setmeal,setmealVO);
            setmealVO.setSetmealDishes(setmealMapper.getSetmealDishes(id));
            setmealVO.setCategoryName(setmealMapper.getById(id).getName());
            return setmealVO;
        }
        return null;
    }

    /**
     * 批量删除套餐
     * @param ids
     */
    public void deleteIds(List<Long> ids) {

        for (Long id : ids) {
            int i = setmealMapper.isExistSeal(id);
            if(i>0){
                throw new RuntimeException("有套餐正在售卖中，不能删除");
            }
        }
        for (Long id : ids){
            setmealMapper.deleteById(id);
        }

    }

    /**
     * 套餐起售停售
     * @param status
     * @param id
     */
    public void startOrStop(Integer status, Long id) {
        Setmeal setmeal=setmealMapper.getById(id);
        setmeal.setStatus(setmeal.getStatus()==1?0:1);
        setmealMapper.startOrStop(setmeal);
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    public void update(SetmealDTO setmealDTO) {

        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
        setmealMapper.deleteSetmealDish(setmeal.getId());
        for (SetmealDish setmealDish : setmealDTO.getSetmealDishes()) {
            setmealDish.setSetmealId(setmeal.getId());
            setmealMapper.insertSetmealDish(setmealDish);
        }


    }


}
